package com.rosesoft.module.mail;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import com.rosesoft.frame.utils.SendMail;

/**
 * @FileName MailThread.java<被观察者>
 * @Description: 
 * @author rose
 * @version 1.0
 * <p>Company: ROSE-SOFT</p>
 * <p>Copyright: Copyright (c) 2011</p>
 */
public class MailThread implements Runnable{
	
	private List emailList;
	private List successList;
	private List failList;
	private StringBuffer contentSb;
	private SendMail sendMail;
	private StringBuffer successSb;
	private StringBuffer failSb;
	public static final String NAME = "邮件发送线程";
	public static final String START = "开启";
	public static final String SHUTDOWN = "关闭";
	private int a;
	public MailThread(){
		
	}
	public MailThread(List emailList,List successList,List failList,StringBuffer contentSb,StringBuffer successSb,StringBuffer failSb,SendMail sendMail,int a){
		this.emailList = emailList;
		this.successList = successList;
		this.failList = failList;
		this.contentSb = contentSb;
		this.failSb = failSb;
		this.successSb = successSb;
		this.sendMail = sendMail;
		this.a = a;
	}
	
	public void run() {
		System.out.println("["+NAME+a+START+"]");
		this.sentMail();
		System.out.println("["+NAME+a+SHUTDOWN+"]");
	}
	
	public void sentMail(){
		String emailAdd = "";
		for (int i = 0; i < emailList.size(); i++) {
			emailAdd = emailList.get(i) == null ? "" : emailList.get(i).toString();
			System.out.println(contentSb.toString());
			boolean sendOk = sendMail.sendMails(emailAdd.trim(), contentSb.toString());
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (sendOk) {
				String successStr = "["+emailAdd+"] [success]\n</br>";
				successSb.append(successStr);
				successList.add(successStr);
				try {
					writeSuccessToDisk("[" + emailAdd + "] [success]\n");
				} catch (Exception e) {
					System.out.println("success 写入异常");
				}
			}else {
				String errorStr = "["+emailAdd+"] [fail]\n</br>";
				failSb.append(errorStr);
				failList.add(errorStr);
				System.out.println("["+emailAdd+"] [fail]");
				try {
					writeFailToDisk("[" + emailAdd + "] [fail]\n");
				} catch (Exception e) {
					System.out.println("fail 写入异常");
				}
				
			}
		}
	}
	public synchronized void writeSuccessToDisk(String str){
		OutputStream os = null;
		OutputStreamWriter osw = null;
		try {
			os = new FileOutputStream("F:\\email_info\\success_mail.txt",true);
			osw = new OutputStreamWriter(os);
			try {
				osw.write(str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				osw.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public synchronized void writeFailToDisk(String str){
		OutputStream os = null;
		OutputStreamWriter osw = null;
		try {
			os = new FileOutputStream("F:\\email_info\\fail_mail.txt",true);
			osw = new OutputStreamWriter(os);
			try {
				osw.write(str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				osw.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
	}
}
